package utils;

import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import ui.Application;

/**
 * CommonUtil is an utility class which stores methods for dealing with versatile tiny tasks not bounded to an application's business logic
 * @author Cipri Lucaci
 * @author Rustem Bekmukhametov
 * @author Alba Xhani
 */
public class CommonUtil {
	private static Logger logger = LogManager.getLogger(Application.class.getName()); // Logger object
	
	public static Level getLoggingLevel(String levelName){
		if(levelName.equalsIgnoreCase("all"))
			return Level.ALL;
		else if(levelName.equalsIgnoreCase("error"))
			return Level.ERROR;
		else if(levelName.equalsIgnoreCase("debug"))
			return Level.DEBUG;
		else if(levelName.equalsIgnoreCase("fatal"))
			return Level.FATAL;
		else if(levelName.equalsIgnoreCase("info"))
			return Level.INFO;
		else if(levelName.equalsIgnoreCase("warn"))
			return Level.WARN;
		else if(levelName.equalsIgnoreCase("off"))
			return Level.OFF;
		return null;
	}
	
	public static String convertStreamToString(java.io.InputStream is) {
	    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
	    return s.hasNext() ? s.next() : "";
	}
	
	public static byte[] stringToBytesASCII(String str) {
		byte[] b = new byte[str.length()];
		for (int i = 0; i < b.length; i++) {
			b[i] = (byte) str.charAt(i);
		}
		return b;
	}
}
